<template>
  <div>
    <!-- 自动生成配置 2018/7/2 zhangQ -->
    <!--<Button type="primary" @click="startGeneration">生成</Button>-->
    <Alert>
      <Button type="primary" style="margin-left: 8px" @click="saveConfig">保存配置</Button>
      <Button type="primary" @click="modalOpera.checkConfigShow=true">选择配置</Button>
    </Alert>
    <Form :model="generationForm" :label-width="120" inline style="width: 700px">
      <!--<FormItem label="表名：" class="FormItemClass">-->
      <!--<i-input v-model="generationForm.tableName" placeholder="表名" style="width: 280px"></i-input>-->
      <!--</FormItem>-->
      <!--<FormItem label="Java实体：" class="FormItemClass">-->
      <!--<i-input v-model="generationForm.domainName" placeholder="Java实体类名" style="width: 280px"></i-input>-->
      <!--</FormItem>-->
      <!--<FormItem label="主键（选填）：" class="FormItemClass">-->
      <!--<i-input v-model="generationForm.primaryKey" style="width: 280px"></i-input>-->
      <!--</FormItem>-->
      <FormItem label="项目所在目录：" class="FormItemClass">
        <i-input v-model="generationForm.projectdir" style="width: 350px">
          <span slot="append" @click="checkProjectDir" style="cursor: pointer">选择</span>
        </i-input>
        <!--<Button @click="checkProjectDir">选择</Button>-->
      </FormItem>
      <br/>
      <FormItem label="实体类包名：" class="FormItemClass">
        <i-input v-model="generationForm.domainPackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.domainPackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="controller包名：" class="FormItemClass">
        <i-input v-model="generationForm.controllerPackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.controllerPackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="service包名：" class="FormItemClass">
        <i-input v-model="generationForm.servicePackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.servicePackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="Repository包名称：" class="FormItemClass">
        <i-input v-model="generationForm.repositoryPackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.repositoryPackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="Mapper接口包：" class="FormItemClass">
        <i-input v-model="generationForm.MapperPackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.MapperPackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="xml映射文件：" class="FormItemClass">
        <i-input v-model="generationForm.mybatisXmlName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.mybatisXmlDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="工具接口包：" class="FormItemClass">
        <i-input v-model="generationForm.utilsPackageName" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="存放目录：" class="FormItemClass">
        <i-input v-model="generationForm.utilsPackageDir" style="width: 280px"></i-input>
      </FormItem>
      <FormItem label="生成文件的编码：" class="FormItemClass">
        <Select v-model="generationForm.unicodeType" style="width: 280px">
          <Option value="UTF-8">UTF-8</Option>
        </Select>
      </FormItem>
      <!--<FormItem label="其他选项：">-->
      <!--<Checkbox v-model="generationForm.annotationJpa">jpa注解</Checkbox>-->
      <!--<Checkbox v-model="generationForm.annotationJpa">jpa注解</Checkbox>-->
      <!--</FormItem>-->
      <br>

    </Form>

    <wtModal v-model="modalOpera.saveConfigShow" title="保存配置" className="vertical-center-modal" width="500px;">
      <saveConfig @close="modalOpera.saveConfigShow=false" :generationForm="generationForm"></saveConfig>
    </wtModal>

    <wtModal v-model="modalOpera.checkConfigShow" title="选择配置" className="vertical-center-modal" width="500px;">
      <generationConfigList v-if="modalOpera.checkConfigShow" @close="modalOpera.checkConfigShow=false" @generationConfig="checkGenerationConfig"></generationConfigList>
    </wtModal>
    <div class="footer">
      <Button type="success" @click="checkedConfig" style="width: 810px">生成</Button>
    </div>
  </div>
</template>

<script>
  import wtModal from '@/components/wtModal'
  import checkTables from '../components/checkTables'
  // import { camelCaseName } from '@/components/common/utils'
  import _ from 'lodash'
  import saveConfig from '../components/saveConfig'
  import generationConfigList from '../components/generationConfigList'
  // import {initTemplates} from '../components/generationUtil'
  // import { mapGetters } from 'vuex'

  export default {
    name: 'index',
    data () {
      let modalOpera = {
        saveConfigShow: false,
        checkTablesShow: false,
        checkConfigShow: false
      }
      let generationForm = {
        tableName: '', // 表名称
        domainName: '', // 实体类名称
        primaryKey: '', // 主键
        projectdir: '', // 项目路径
        domainPackageName: '', // 实体包名称
        domainPackageDir: '', // 实体包路径
        controllerPackageName: '', // 控制器包名
        controllerPackageDir: '', // 控制器路径
        servicePackageName: '', // 业务层包名称
        servicePackageDir: '', // 业务层包路径
        MapperPackageName: '', // mapper包名称
        MapperPackageDir: '', // mapper包路径
        mybatisXmlName: '', // mapperXML名称
        mybatisXmlDir: '', // mapperXML路径
        repositoryPackageName: '', // jpa respository包名称
        repositoryPackageDir: '', // jpa respository包路径
        utilsPackageName: '', // 工具包名称
        utilsPackageDir: '', // 工具包路径
        annotationJpa: '', // jpa注解
        unicodeType: 'UTF-8'
      }
      let datas = { modalOpera, generationForm }
      return datas
    },
    watch: {
      // table (val, oldval) {
      //   if (val) {
      //     console.log('vtableal', val)
      //     this.generationForm.tableName = val.tableName
      //     this.generationForm.domainName = camelCaseName(val.tableName, true)
      //     let primaryKeyNameArray = []
      //     let primaryKeys = _.filter(val.columns, (o) => {
      //       if (o.primaryKey) {
      //         primaryKeyNameArray.push(o.name)
      //       }
      //       return o.primaryKey
      //     })
      //     if (primaryKeys && primaryKeys.length > 0) {
      //       this.generationForm.primaryKey = primaryKeyNameArray.join(',')
      //     }
      //   }
      // },
      // generationForm: {
      //   handler: function (val, oldval) {
      //     if (val.controllerPackageName !== oldval.controllerPackageName) {
      //       val.controllerPackageDir = '/src/main/java/' + val.controllerPackageName.replace(/\./g, '/')
      //     }
      //     if (val.servicePackageName !== oldval.servicePackageName) {
      //       val.servicePackageDir = '/src/main/java/' + val.servicePackageName.replace(/\./g, '/')
      //     }
      //     if (val.MapperPackageName !== oldval.MapperPackageName) {
      //       val.MapperPackageDir = '/src/main/java/' + val.MapperPackageName.replace(/\./g, '/')
      //     }
      //     if (val.mybatisXmlName !== oldval.mybatisXmlName) {
      //       val.mybatisXmlDir = '/src/main/java/' + val.mybatisXmlName.replace(/\./g, '/')
      //     }
      //     if (val.repositoryPackageName !== oldval.repositoryPackageName) {
      //       val.repositoryPackageDir = '/src/main/java/' + val.repositoryPackageName.replace(/\./g, '/')
      //     }
      //     if (val.utilsPackageName !== oldval.utilsPackageName) {
      //       val.utilsPackageDir = '/src/main/java/' + val.utilsPackageName.replace(/\./g, '/')
      //     }
      //   },
      //   deep: true
      // },
      'generationForm.domainPackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.domainPackageDir || temp === this.generationForm.domainPackageDir) {
          this.generationForm.domainPackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.controllerPackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.controllerPackageDir || temp === this.generationForm.controllerPackageDir) {
          this.generationForm.controllerPackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.servicePackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.servicePackageDir || temp === this.generationForm.servicePackageDir) {
          this.generationForm.servicePackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.MapperPackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.MapperPackageDir || temp === this.generationForm.MapperPackageDir) {
          this.generationForm.MapperPackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.mybatisXmlName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.mybatisXmlDir || temp === this.generationForm.mybatisXmlDir) {
          this.generationForm.mybatisXmlDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.repositoryPackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.repositoryPackageDir || temp === this.generationForm.repositoryPackageDir) {
          this.generationForm.repositoryPackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      },
      'generationForm.utilsPackageName' (val, oldval) {
        let temp = '/src/main/java/' + oldval.replace(/\./g, '/')
        if (!this.generationForm.utilsPackageDir || temp === this.generationForm.utilsPackageDir) {
          this.generationForm.utilsPackageDir = '/src/main/java/' + val.replace(/\./g, '/')
        }
      }
    },
    // computed: {
    //   // ...mapGetters(['generation/tables']),
    //   table () {
    //     return this.$store.state.generation.tables[0]
    //   }
    // },
    methods: {
      /**
       * 选择配置
       * */
      checkedConfig () {
        this.$emit('checkedConfig', _.cloneDeep(this.generationForm))
      },
      /**
       * TODO(选择项目所在路径)
       * @auther zhangQ
       * @date 2018/4/28 18:51
       */
      checkProjectDir () {
        let remote = this.$electron.remote
        let dialog = remote.dialog
        // elecctron弹框
        dialog.showOpenDialog({ properties: ['openFile', 'openDirectory', 'multiSelections'] }, (res) => {
          if (res) {
            this.generationForm.projectdir = res[0]
          }
        })
      },
      /**
       * TODO(保存配置)
       * @param :::
       * @auther zhangQ
       * @date 2018/5/21 19:31
       */
      saveConfig () {
        this.modalOpera.saveConfigShow = true
      },
      /**
       * TODO(选择数据生成配置)
       * @auther zhangQ
       * @date 2018/5/3 11:53
       */
      checkGenerationConfig (config) {
        let generationForm = {
          // tableName: '',
          // domainName: '',
          // primaryKey: '',
          projectdir: config.projectdir,
          domainPackageName: config.domainPackageName,
          domainPackageDir: config.domainPackageDir,
          controllerPackageName: config.controllerPackageName,
          controllerPackageDir: config.controllerPackageDir,
          servicePackageName: config.servicePackageName,
          servicePackageDir: config.servicePackageDir,
          MapperPackageName: config.MapperPackageName,
          MapperPackageDir: config.MapperPackageDir,
          mybatisXmlName: config.mybatisXmlName,
          mybatisXmlDir: config.mybatisXmlDir,
          repositoryPackageName: config.repositoryPackageName,
          repositoryPackageDir: config.repositoryPackageDir,
          utilsPackageName: config.utilsPackageName,
          utilsPackageDir: config.utilsPackageDir,
          unicodeType: 'UTF-8'
        }
        this.generationForm = Object.assign({}, this.generationForm, generationForm)
      }
    },
    created () {
    },
    mounted () {
    },
    components: { wtModal, checkTables, saveConfig, generationConfigList }
  }
</script>

<style scoped>
  .FormItemClass {
    width: 280px;
  }
</style>